﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using SLLottery.Common;
using SLLottery.LotterySrv;
namespace SLLottery.Controls
{
    public partial class MainBet : UserControl
    {
        public string LotteryNum
        {
            get;
            set;
        }
        public BallSize BallSize
        {
            get;
            set;
        }
        public List<LotteryNumsInfo> DataSource
        {
            get { return (List<LotteryNumsInfo>)this.LotteryNums.LotteryNumsGrid.ItemsSource; }
            set
            {
                this.LotteryNums.DataSource = value;
            }
        }
        private LotterySrv.LotteryServiceClient srv;
        public MainBet()
        {
            InitializeComponent();
            srv = WcfProxy.GetProxy;
            this.gridLottery.LoadingRow += new EventHandler<DataGridRowEventArgs>(gridLottery_LoadingRow);
            srv.GetBetRecordCompleted += new EventHandler<GetBetRecordCompletedEventArgs>(srv_GetBetRecordCompleted);
            srv.GetTopTenWinInfoCompleted += new EventHandler<GetTopTenWinInfoCompletedEventArgs>(srv_GetTopTenWinInfoCompleted);
            this.Loaded += new RoutedEventHandler(MainBet_Loaded);
        }
        void MainBet_Loaded(object sender, RoutedEventArgs e)
        {
            RefreshGrid();
        }
        void gridLottery_LoadingRow(object sender, DataGridRowEventArgs e)
        {
        }
        public void RefreshGrid()
        {
            srv.GetBetRecordAsync(App.Session[Constant.USERNAME_SESSION].ToString());
            srv.GetTopTenWinInfoAsync((int)MainPage.lotteryTypeAll);
        }
        void srv_GetBetRecordCompleted(object sender, GetBetRecordCompletedEventArgs e)
        {
            this.gridLottery.ItemsSource = e.Result.ToList();
        }

        void srv_GetTopTenWinInfoCompleted(object sender, GetTopTenWinInfoCompletedEventArgs e)
        {
            this.lbxTop10Winner.ItemsSource = e.Result.ToList();
        }
        public void CreateElement(string LotteryNum, BallSize BallSize, List<LotteryNumsInfo> DataSource)
        {
            //KaijiangContainer LotteryNums = new KaijiangContainer();
            //bord_Lottery.Child = LotteryNums;
            //LotteryNums.LotteryNumsGrid.ItemsSource = DataSource;
            LotteryNums.CreateElement(LotteryNum, BallSize);
        }
        public void CreateElement()
        {
            //KaijiangContainer LotteryNums = new KaijiangContainer();
            //bord_Lottery.Child = LotteryNums;
            //LotteryNums.LotteryNumsGrid.ItemsSource = DataSource;
            LotteryNums.CreateElement(LotteryNum, BallSize);
        }
        public void LoadData()
        {
            LotteryNums.LoadData();
        }
        private void lbxTop10Winner_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.lbxTop10Winner.SelectedIndex != -1)
            {
                DealInfoWindow dw = new DealInfoWindow((lbxTop10Winner.SelectedItem as BetRecord).RecordID);
                this.lbxTop10Winner.SelectedIndex = -1;
                dw.isTopTen = true;
                dw.Show();
            }
        }
    }
    public class DirectionConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (System.Convert.ToInt32(value) == 0)
                return "正向";
            else if (System.Convert.ToInt32(value) == 1)
                return "反向";
            else
                return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }
}
